package com.shengde.jinwawaforleader.model.personalCenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.activity.LoginActivity;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.utils.MD5;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ModifySecret extends BaseActivity {
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private TextView textView;
    private TextView tv_back;
    private TextView tv_title;
    private Button login;
    public static String string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        findViewById(R.id.login).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                init();
            }
        });
    }

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_secret);
    }

    @Override
    public void initView() {
        textView = (TextView) findViewById(R.id.textView1);
        editText1 = (EditText) findViewById(R.id.editText_lun1);
        editText2 = (EditText) findViewById(R.id.editText_lun2);
        editText3 = (EditText) findViewById(R.id.editText_lun3);
        login = (Button) findViewById(R.id.login);
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
    }

    @Override
    public void initData() {
        textView.setText("当前账号是：" + User.getUserPhone(getApplication()));
        tv_title.setText(getString(R.string.modify_secret));
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
        login.setOnClickListener(this);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
        }
    }

    /**
     * 判断电话号码是否符合格式.
     *
     * @param inputText the input text
     * @return true, if is phone
     */
    public static boolean isCorrectPhone(String inputText) {
        Pattern p = Pattern.compile("^((14[0-9])|(13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))\\d{8}$");
        Matcher m = p.matcher(inputText);
        return m.matches();
    }

    private void init() {
        final String content1 = editText1.getText().toString().trim();
        final String content2 = editText2.getText().toString().trim();
        final String content3 = editText3.getText().toString().trim();
        if (TextUtils.isEmpty(content1)) {
            Toast.makeText(ModifySecret.this, "旧密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (content1.equals(User.getMiMa(getApplication()))) {
            if (Pattern.matches("^[^\\t\\n\\x0B\\f\\r]{6,20}$", content2) && Pattern.matches("^[^\\t\\n\\x0B\\f\\r]{6,20}$", content3)) {
                if (content2.equals(content3)) {
                    String url = "";
                    if (User.isTest) {
                        url = BaseProtocol.HOST_BASE_TEST + "/parent/PhoneValidate";
                    } else {
                        url = BaseProtocol.HOST_BASE + "/parent/PhoneValidate";
                    }
                    RequestParams params = BaseProtocol.getRequestParams(url);
                    params.addParameter("userid", User.getUserLoginAccount(getApplication()));
                    if (isCorrectPhone(User.getUserLoginAccount(getApplicationContext()))) {
                        params.addParameter("oldpassword", MD5.getMD5(content1));
                        params.addParameter("newpassword", MD5.getMD5(content3));
                    } else {
                        params.addParameter("oldpassword", content1);
                        params.addParameter("newpassword", content3);
                    }
                    Log.e("TAG", "params=" + params.toString());
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {
                        }

                        @Override
                        public void onFinished() {
                        }
                    });
                    User.setIsOnline(ModifySecret.this, false);
                    Intent intent = new Intent();
                    intent.setClass(ModifySecret.this, LoginActivity.class);
                    startActivity(intent);
                    User.logout(getApplication());
                    finish();

                } else {
                    Toast.makeText(ModifySecret.this, "请输入相同的密码", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(ModifySecret.this, "密码由6~20位的数字,字母或符号组成", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(ModifySecret.this, "密码错误", Toast.LENGTH_SHORT).show();
        }


    }

    public static void device_token(String device_token) {
        // TODO Auto-generated method stub
        string = device_token;
    }
}